package com.auxgroup.smarthome.business.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auxgroup.bridge.app.inner.vo.DeviceModelSimpleVo;
import com.auxgroup.smarthome.business.dto.DeviceModelInfoDto;
import com.auxgroup.smarthome.business.service.inner.DeviceModelInfoInnerService;
import com.auxgroup.smarthome.business.vo.DeviceModelInfoVo;
import com.auxgroup.smarthome.utils.PageContent;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by niuGuangzhe on 2017/8/14.
 */
@Api(description = "设备型号管理", tags = "设备型号管理")
@RestController
public class DeviceModelInfoController {

    @Autowired
    private DeviceModelInfoInnerService deviceModelInfoInnerService;

    @ApiOperation(value = "添加设备型号", notes = "添加设备型号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "设备型号", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "modelItem", value = "内机物料描述", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "modelDescribe", value = "设备型号描述", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "advertise", value = "设备广告描述", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "suitType", value = "适用类型0：家用 1：商用", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "useType", value = "挂机柜机标识0：挂机 1：柜机", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "deviceMainUri", value = "设备主模块url", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "entityUri", value = "设备型号url", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "stepUri", value = "配网步骤url", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "step", value = "配网步骤json", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "deviceType", value = "新旧设备标识0：老设备 1：新设备", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "feature", value = "功能型号列表json", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "category", value = "型号类别：0：挂机，1：柜机，2：单元机，3：多联机", paramType = "form", dataType = "string", required = true)
    })
    @RequestMapping(value = "/products/model", method = RequestMethod.POST)
    public ApiResponse addDeviceModelInfo(String model, String modelItem,
                                          @Valid @ApiIgnore DeviceModelInfoDto dto) {
//        return deviceModelInfoInnerService.addDeviceModelInfo(model, modelItem, dto.toString());
        return deviceModelInfoInnerService.addDeviceModelInfo(model, modelItem, JSON.toJSONString(dto));
    }
    /**
     * 型号类别：
     * 0：挂机
     * 1：柜机
     * 2：单元机
     * 3：多联机
     */
    @ApiOperation(value = "修改设备型号", notes = "修改设备型号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "设备型号主键", paramType = "path", dataType = "string", required = true),
            @ApiImplicitParam(name = "model", value = "设备型号", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "modelDescribe", value = "设备型号描述", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "advertise", value = "设备广告描述", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "suitType", value = "适用类型0：家用 1：商用", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "useType", value = "挂机柜机标识0：挂机 1：柜机", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "deviceMainUri", value = "设备主模块url", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "entityUri", value = "设备型号url", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "stepUri", value = "配网步骤url", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "step", value = "配网步骤json", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "deviceType", value = "新旧设备标识0：老设备 1：新设备", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "feature", value = "功能型号列表json", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "category", value = "型号类别：0：挂机，1：柜机，2：单元机，3：多联机", paramType = "form", dataType = "string", required = true)
    })
    @RequestMapping(value = "/products/models/{modelId}", method = RequestMethod.PUT)
    public ApiResponse updateDeviceModelInfo(@PathVariable("modelId") String modelId,
                                             @Valid @ApiIgnore DeviceModelInfoDto dto) {
        return deviceModelInfoInnerService.updateDeviceModelInfo(modelId, JSONObject.toJSONString(dto));
    }

    @ApiOperation(value = "获取设备型号", notes = "获取设备型号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "设备型号", paramType = "path", dataType = "string", required = true),
    })
    @RequestMapping(value = "/products/models/{model}", method = RequestMethod.GET)
    public ApiResponse getDeviceModelInfo(@PathVariable("model") String model) {
        return deviceModelInfoInnerService.getDetailDeviceModelInfo(model);
    }

    @ApiOperation(value = "获取设备型号", notes = "获取设备型号")
    @RequestMapping(value = "/products/models/one", method = RequestMethod.GET)
    public ApiResponse getDeviceModelInfoByOne(@RequestParam("model") String model) {
        return deviceModelInfoInnerService.getDetailDeviceModelInfo(model);
    }

    @ApiOperation(value = "获取设备型号列表", notes = "获取设备型号列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "设备型号名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "modelItem", value = "设备物料名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", dataType = "string"),
    })
    @RequestMapping(value = "/products/models", method = RequestMethod.GET)
    public ApiResponse<Page<DeviceModelInfoVo>> getDeviceModelInfos(@RequestParam(value = "model", required = false) String model,
                                                                    @RequestParam(value = "modelItem", required = false) String modelItem,
                                                                    @RequestParam(value = "page", required = false) Integer page,
                                                                    @RequestParam(value = "size", required = false) Integer size) {
        ApiResponse<PageContent<DeviceModelInfoVo>> pageContentApiResponse =  deviceModelInfoInnerService.getDetailDeviceModelInfos(model, modelItem, page, size);
        if(!pageContentApiResponse.isNotErrorCode()) {
            return (ApiResponse)pageContentApiResponse;
        }
        return ApiResponse.ok(pageContentApiResponse.getData().convertPage());
    }

    @ApiOperation(value = "删除设备型号",notes = "删除设备型号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "型号主键", paramType = "query", dataType = "string", required = true)
    })
    @RequestMapping(value = "/products/model", method = RequestMethod.DELETE)
    public ApiResponse deleteDeviceModelInfo(String modelId) {
        return deviceModelInfoInnerService.deleteDeviceModelInfo(modelId);
    }

    @ApiOperation(value = "关键字查询内机物料列表",notes = "查询内机物料列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword",value = "关键字",paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "integer", required = false),
            @ApiImplicitParam(name = "size", value = "分页大小", paramType = "query", dataType = "integer", required = false)
    })
    @RequestMapping(value = "/product/model_items",method = RequestMethod.GET)
    public ApiResponse getModelItemByKeywords(String keyword,
                                              @RequestParam(defaultValue = "0", required = false) Integer page,
                                              @RequestParam(defaultValue = "100", required = false) Integer size){
        return deviceModelInfoInnerService.getModelItemsByKeyword(keyword,page,size);
    }

    @ApiOperation(value ="返回所有的型号信息",notes = "返回所有的型号信息")
    @RequestMapping(value = "/products/all_models",method = RequestMethod.GET)
    public ApiResponse<List<DeviceModelSimpleVo>> getAllModelInfo(){
        return deviceModelInfoInnerService.getAllDeviceModelInfo();
    }


    @ApiOperation(value ="根据主键返回设备型号信息",notes = "根据主键返回设备型号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId",value = "型号主键",paramType = "query",dataType = "string",required = true)
    })
    @RequestMapping(value = "/products/model_by_id",method = RequestMethod.GET)
    public ApiResponse getModelInfoByModelId(String modelId){
        return deviceModelInfoInnerService.getDeviceModelInfoByModelId(modelId);
    }

    @ApiOperation(value ="查询型号名称是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model",value = "型号名称",paramType = "query",dataType = "string",required = true)
    })
    @GetMapping(value = "/is_exists_model_name")
    public ApiResponse isExistsModelName(String model) {
        return ApiResponse.ok(deviceModelInfoInnerService.isExistsModelName(model));
    }


}
